package com.ee.chat.admin.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ee.chat.admin.service.IBannerService;
import com.ee.chat.admin.service.IOperationService;
import com.ee.chat.common.bean.Result;
import com.ee.chat.common.dao.BannerMapper;
import com.ee.chat.common.entity.Banner;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author AppiJ 2019年11月12日 17:48
 */
@Service
public class BannerServiceImpl implements IBannerService {

    @Resource
    BannerMapper bannerMapper;
    @Resource
    IOperationService operationService;

    /**
     * 获取banner列表
     * @author AppiJ
     * @date 2019年11月12日 17:56:38
     * @param
     * @return
     */
    @Override
    public Result list(HttpServletRequest request, Integer page, Integer limit) throws Exception {

        Map<String, Object> data = new HashMap<>();

        Page<Map<String, Object>> p = new Page<>(page, limit);
        IPage<Map<String, Object>> iPage = bannerMapper.list(p);
        List<Map<String, Object>> gifts = iPage.getRecords();

        data.put("banners", gifts);
        data.put("total", iPage.getTotal());

        operationService.addOperationLog(request, "获取banner数据列表");

        return Result.returnSuccess(data);
    }

    /**
     * 编辑/添加banner
     * @author AppiJ
     * @date 2019年11月14日 14:49:44
     * @param banner
     * @return
     */
    @Override
    public Result add(HttpServletRequest request, Banner banner) throws Exception {

        if(banner.getId() == null){
            //添加banner
            bannerMapper.insert(banner);

            operationService.addOperationLog(request, "新增banner，id："+banner.getId());
        }else {
            //修改banner
            bannerMapper.updateById(banner);

            operationService.addOperationLog(request, "编辑banner，id："+banner.getId());
        }

        return Result.returnSuccess(null);
    }

    /**
     * 去编辑banner
     * @author AppiJ
     * @date 2019年11月14日 14:53:29
     * @param id
     * @return
     */
    @Override
    public Result toEdit(HttpServletRequest request, Integer id) throws Exception {

        Map<String, Object> data = new HashMap<>();

        Banner banner = bannerMapper.selectById(id);
        banner = banner == null ? new Banner() : banner;

        data.put("banner", banner);

        return Result.returnSuccess(data);
    }

    /**
     * 禁用/启用banner
     * @author AppiJ
     * @date 2019年11月14日 14:57:16
     * @param id
     * @return
     */
    @Override
    public Result updateStatus(HttpServletRequest request, Integer id) throws Exception {

        Banner banner = bannerMapper.selectById(id);
        if(banner.getStatus() == 0){
            banner.setStatus(1);
        }else {
            banner.setStatus(0);
        }
        bannerMapper.updateById(banner);

        operationService.addOperationLog(request, "变更banner状态，id："+banner.getId());

        return Result.returnSuccess(null);
    }
}
